package com.lf125.plus.plugin.LF125PlusPlugin;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class MyService extends Service {

    private LF125KTagReadThread lf;
    private boolean isRunning = false;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("CardReaderService", "Service created");
        // 初始化LF125KTagReadThread，设置回调处理器
        lf = new LF125KTagReadThread();
        lf.setHandler(handler);
        lf.init();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("CardReaderService", "Service started");
        if (!isRunning) {
            isRunning = true;
            lf.startRead();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("CardReaderService", "Service destroyed");
        if (lf != null) {
            lf.stopRead();
            lf.close();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // 处理从LF125KTagReadThread返回的数据
            if (msg.what == LF125KTagReadThread.MSG_RESULT) {
                long id = msg.getData().getLong("id");
                int country = msg.getData().getInt("country");
                Log.d("CardReaderService", "ID: " + id + ", Country: " + country);
            }
        }
    };
}

